package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.dto.TProcessValueDto;
import com.woniu.car.dto.constant.CommonConstant;
import com.woniu.car.dto.TProcessValueQueryDto;
import com.woniu.car.entity.TProcess;
import com.woniu.car.entity.TProcessValue;
import com.woniu.car.entity.TServerDetail;
import com.woniu.car.exception.ParamException;
import com.woniu.car.exception.ServiceSException;
import com.woniu.car.mapper.TProcessValueMapper;
import com.woniu.car.param.TProcessValueParam;
import com.woniu.car.param.TProcessValueUpdateParam;
import com.woniu.car.service.TProcessService;
import com.woniu.car.service.TProcessValueService;
import com.woniu.car.service.TServerDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务流程字段表 服务实现类
 * </p>
 *
 * @author *********
 * @since 2021-06-21
 */
@Service
public class TProcessValueServiceImpl extends ServiceImpl<TProcessValueMapper, TProcessValue> implements TProcessValueService {

    @Resource
    private TProcessService tProcessService;
    @Resource
    private TServerDetailService tServerDetailService;

    @Override
    @Transactional
    public List<TProcessValueDto> insertValue(Integer detailId, Integer id) throws Exception {

        TServerDetail tServerDetail = tServerDetailService.getById(detailId);

        if (null == tServerDetail) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        Integer serviceId = tServerDetail.getServerId();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("service_id",serviceId);
        wrapper.eq("status",CommonConstant.ZERO);
        List<TProcess> list = tProcessService.list(wrapper);

        if (null == list || list.size() <= 0) {
            throw new ParamException(CommonConstant.PROCESSNULL);
        }

        List<TProcessValueDto> tProcessValueDtos = new ArrayList<>();

        for (TProcess tProcess : list) {

            TProcessValue tProcessValue = new TProcessValue();
            tProcessValue.setProcessId(tProcess.getProcessId());
            tProcessValue.setProcessValue(tProcess.getServiceName());
            tProcessValue.setUserId(id);
            tProcessValue.setNob(tProcess.getNob());
            tProcessValue.setDetailId(detailId);
            tProcessValue.setServiceId(serviceId);
            boolean save = this.save(tProcessValue);

            if (!save) {
                throw new ServiceSException(CommonConstant.INSERTWRONG);
            }

            TProcessValue byId = this.getById(tProcessValue.gettValueId());

            TProcessValueDto tProcessValueDto = new TProcessValueDto();
            BeanUtils.copyProperties(byId,tProcessValueDto);
            tProcessValueDtos.add(tProcessValueDto);

        }

        return tProcessValueDtos;
    }

    @Override
    @Transactional
    public TProcessValueDto updateValueStatus(TProcessValueUpdateParam tProcessValueUpdateParam, Integer id) throws Exception {

        if (null == tProcessValueUpdateParam || null == tProcessValueUpdateParam.getTValueId()) {
            throw new ParamException(CommonConstant.RESULTNULL);
        }

        TProcessValue tProcessValue = new TProcessValue();
        Date date = new Date();

        tProcessValue.setRawUpdateTime(date);
        tProcessValue.setUserId(id);
        BeanUtils.copyProperties(tProcessValueUpdateParam,tProcessValue);

        boolean b = this.updateById(tProcessValue);

        if (!b) {
            throw new ServiceSException(CommonConstant.UPDATEWRONG);
        }

        TProcessValue byId = this.getById(tProcessValue.gettValueId());
        TProcessValueDto tProcessValueDto = new TProcessValueDto();
        BeanUtils.copyProperties(byId,tProcessValueDto);

        return tProcessValueDto;
    }

    @Override
    @Transactional
    public List<TProcessValueQueryDto> queryProcessValue(Integer detailId){

        if (null == detailId || detailId <= 0) {
            throw new ParamException(CommonConstant.PROCESSNULL);
        }

        List<TProcessValueQueryDto> tProcessValueQueryDtos = new ArrayList<>();

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("detail_id",detailId);
        List<TProcessValue> list = this.list(wrapper);

        for (TProcessValue tProcessValue : list) {
            TProcessValueQueryDto tProcessValueQueryDto = new TProcessValueQueryDto();
            BeanUtils.copyProperties(tProcessValue,tProcessValueQueryDto);
            tProcessValueQueryDtos.add(tProcessValueQueryDto);
        }

        return tProcessValueQueryDtos;
    }

}
